from fastapi import APIRouter, Request, Body
from pydantic import BaseModel
from finance.python.middleware.auth import token_required
from finance.python.middleware.logging import log_required
from finance.python.services import user_setting_service


user_setting_router = APIRouter(
    prefix="/api",
    tags=["user_setting"]
)


class UpdatePasswordRequest(BaseModel):
    current_password: str
    new_password: str


@user_setting_router.post(path='/update_password', summary="修改密码")
@token_required
@log_required
async def register(request: Request, update_password_data: UpdatePasswordRequest = Body(...)):
    """
    修改密码
    :param request: 请求对象
    :param update_password_data: 修改密码请求数据
    :return: 修改密码结果
    """
    user_id = request.state.user_id
    return await user_setting_service.update_password(
        user_id,
        update_password_data.current_password,
        update_password_data.new_password
    )


class UpdatePhoneRequest(BaseModel):
    current_password: str
    new_phone: str
    verify_code: str


@user_setting_router.post(path='/update_phone', summary="修改手机号")
@token_required
@log_required
async def register(request: Request, update_phone: UpdatePhoneRequest = Body(...)):
    """
    修改手机号
    :param request: 请求对象
    :param update_phone: 修改手机号请求数据
    :return: 修改手机号结果
    """
    user_id = request.state.user_id
    return await user_setting_service.update_phone(
        user_id,
        update_phone.current_password,
        update_phone.new_phone,
        update_phone.verify_code
    )


class UpdateUsernameRequest(BaseModel):
    new_username: str


@user_setting_router.post(path='/update_username', summary="修改用户名")
@token_required
@log_required
async def update_username(request: Request, update_username: UpdateUsernameRequest = Body(...)):
    """
    修改用户名
    :param request: 请求对象
    :param update_username: 修改用户名请求数据
    :return: 修改用户名结果
    """
    user_id = request.state.user_id
    return await user_setting_service.update_username(
        user_id,
        update_username.new_username
    )


@user_setting_router.get(path='/get_user_info', summary="获取用户信息")
@token_required
@log_required
async def get_user_info(request: Request):
    """
    获取用户信息
    :param request: 请求对象
    :return: 用户信息
    """
    user_id = request.state.user_id
    return await user_setting_service.get_user_info(user_id)
